package com.dominFramework.core.builder;

import org.apache.commons.io.IOUtils;

import com.dominFramework.core.builder.asset.DriverManagerOpt;
import com.dominFramework.core.builder.metainfo.DBMetaInfoUtils;
import com.dominFramework.core.builder.resources.DaoBuilder;
import com.dominFramework.core.dao.asset.DBType;
import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.typewrap.Dtos;
import com.dominFramework.core.utils.SystemUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.SQLException;


/**
 * 数据访问层代码生成器事例
 * <p>
 * <p>
 * 提示：已经支持可视化生成。请访问：开发工具箱->WebIDE集成开发。
 *
 * @author dl
 * @throws SQLException
 */
public class DaoConsoleDemo {

    public static void createDao(DriverManagerOpt driverOpt, Dto dto) throws SQLException, IOException {


        if (SystemUtils.isEmpty(driverOpt)) {
            //当前版本支持mysql、oracle、sqlserver2005+、H2
            driverOpt = new DriverManagerOpt();
            driverOpt.setDataBaseType(DBType.MYSQL);
            driverOpt.setIp("122.114.236.45");
            driverOpt.setPort("3306");
            //数据库名或数据库实例名
            driverOpt.setCatalog("ayzz");
            driverOpt.setUserName("root");
            driverOpt.setPassword("1qaz2wsx");
        }

        if (SystemUtils.isEmpty(dto)) {
            dto = Dtos.newDto();
            //改为自己存放相关文件的磁盘文件路径
            //dto.put("outPath", "G:/01.AOS/03.AOS_V2/src/dao/java/aos/system/dao");
            //dto.put("outPath", "D:/workspace/dl/orisper/src/dao/java/com/orisper/dao");
            dto.put("outPath", "D:/demo/");
            //改为自己相关文件的包路径
            dto.put("package", "dao");
            //作者
            dto.put("author", "dominator_ap");
            //四个模板所在路径
            //指定多张表请用逗号分隔，不指定则全数据库生成不包括aos开头表和demo开头表；
            //!!表名区分大小写的喔
            //dto.put("tables", "aos_module, aos_org, aos_role, aos_role_module, aos_user_role,aos_cmp,aos_icon");
            //dto.put("tables", "demo_account");
            //===================
        }

        if (SystemUtils.isEmpty(dto.getString("PO_JAVA_VM")) || SystemUtils.isEmpty(dto.getString("DAO_JAVA_VM")) || SystemUtils.isEmpty(dto.getString("DAO_XML_VM"))) {

            dto.put("PO_JAVA_VM","/src/main/resources/vm/po.java.vm");
			dto.put("DAO_JAVA_VM","/src/main/resources/vm/dao.java.vm");
			dto.put("DAO_XML_VM","/src/main/resources/vm/dao.xml.vm");
        }

        Connection connection = DBMetaInfoUtils.newConnection(driverOpt);
        DaoBuilder.buildDao(connection, dto);
        connection.close();
    }






    public static void main(String[] args) throws SQLException, IOException {
        DaoConsoleDemo.createDao(null, null);
    }

}
